dim args
args = inputbox("size=?",args,0.3)

if args="" then args=0.3
Dim size
size = cdbl(trim(args))'application.Gui.InputBox("What size text", size, 0.3) 

Dim doc
Set doc = application.ActiveDocument
main

' Function to validate document
Private Function ValidateServer(docObj)
    Dim key, licenseServer, licenseToken
    key = docObj.Validate(0)
    Set licenseServer = CreateObject("MGCPCBAutomationLicensing.Application")
    licenseToken = licenseServer.GetToken(key)
    Set licenseServer = nothing
    'On Error Resume Next
    Err.Clear
    docObj.Validate(licenseToken)
    If Err Then
        ValidateServer = 0    
    Else 
        ValidateServer = 1
    End If
End Function

sub GetUserLicence(docObj)

	If ValidateServer(docObj) = 0 Then
	    msgbox "Server validation failed."
	    exit sub
	End If
	
end sub

'----------------------------------------------------------------

sub SilkScreenRef_Move(docObj)

    Dim fabtxts 'As FabricationLayerTexts
    Dim fabtxt 'As FabricationLayerText
    Dim cnt, ori, dx, dy, pa, x, y, z, sorted, TheArray, Temp
    Dim place 'As PlacementOutline
    Dim places 'As PlacementOutlines

    
      
    Set fabtxts = docObj.FabricationLayerTexts(epcbFabAll, epcbSelectSelected, epcbSideTopOrBottom, True)  'get a selection of all the fab layer texts
    cnt = fabtxts.Count

    Call application.Gui.ProgressBarInitialize(True, "Progess", cnt, 0) 'initialize the progress bar from o to cnt
    cnt = 0
    docObj.TransactionStart (epcbDRCModeDRC) ' make all the moves one single undable action
    application.LockServer 'speed up the process by locking the server
    For Each fabtxt In fabtxts 'walk through all the fabrication layer texts
        If Not fabtxt.Component Is Nothing Then 'if it is not text belonging to a component throw it out.  Like board level texts    
            fabtxt.Component.Selected = True 'select the component
            'Call docObj.ActiveView.SetExtentsToSelection(False, True) 'zoom into the component        
            Set places = docObj.PlacementOutlines(epcbSelectSelected)
            'docObj.ActiveView.SetExtentsToSelection
            For Each place In places  ' find the outside of the component to move the silkscreen text to
                'pa = place.Geometry.PointsArray
                pa = place.Geometry.Outline           
                sorted = False
                Do While Not sorted
                    sorted = True
                    For x = 0 To UBound(pa, 2) - 1
                        If pa(0, x) > pa(0, x + 1) Then
                            Temp = pa(0, x + 1)
                            pa(0, x + 1) = pa(0, x)
                            pa(0, x) = Temp
                            sorted = False
                        End If
                    Next
                Loop
		        dx =  (pa(0, UBound(pa, 2)) - pa(0, 0)) / 2
				'msgbox dx          
		        sorted = False
                Do While Not sorted
                    sorted = True
                    For y = 0 To UBound(pa, 2) - 1

                        If pa(1, y) > pa(1, y + 1) Then
                            Temp = pa(1, y + 1)
                            pa(1, y + 1) = pa(1, y)
                            pa(1, y) = Temp
                            sorted = False
                        End If
                    Next
                Loop           
                dy =  (pa(1, UBound(pa, 2)) - pa(1, 0)) / 2
            'msgbox dy
            Next       
            ori = fabtxt.Component.Orientation
	    'msgbox ori        
            fabtxt.Format.VerticalJust = epcbJustifyVCenter
            fabtxt.Format.HorizontalJust = epcbJustifyHCenter
            If ori = 0 Or ori = 180 Then
                fabtxt.Format.Orientation(epcbAngleUnitDegrees) = 0 'if the text is rotated at 0 or 180 make it 0
                If fabtxt.Type = epcbFabSilkscreen Then
                    fabtxt.Format.Height(epcbUnitMM) = size
                    Call fabtxt.Move(fabtxt.Component.PositionX, fabtxt.Component.PositionY) 'Offset in y direction            
                End If

                If fabtxt.Type = epcbFabAssembly Then
                    fabtxt.Format.Height(epcbUnitMM) = size 'dy
                    Call fabtxt.Move(fabtxt.Component.PositionX, fabtxt.Component.PositionY) ' + dy, epcbUnitMils)
                End If
            
            Else
                fabtxt.Format.Orientation(epcbAngleUnitDegrees) = 90

                If fabtxt.Type = epcbFabSilkscreen Then
                    fabtxt.Format.Height(epcbUnitMM) = size
                    'Call fabtxt.Move(fabtxt.Component.AssemblyOriginX + dx, fabtxt.Component.AssemblyOriginY, epcbUnitMils)
                    Call fabtxt.Move(fabtxt.Component.PositionX, fabtxt.Component.PositionY) 'Offset in x direction
                End If

                If fabtxt.Type = epcbFabAssembly Then
                    fabtxt.Format.Height(epcbUnitMM) = size 'dx
                    'Call fabtxt.Move(fabtxt.Component.AssemblyOriginX + dx, fabtxt.Component.AssemblyOriginY, epcbUnitMils)
                    Call fabtxt.Move(fabtxt.Component.PositionX, fabtxt.Component.PositionY) ', epcbUnitMils)
                End If
            
            End If
        
            application.Gui.ProgressBar (cnt)
            cnt = cnt + 1
    
            fabtxt.Component.Selected = False
        End If

    Next

    application.UnlockServer 'unlock the server

    docObj.TransactionEnd (True) 'end the make it all one undo
    application.Gui.ProgressBarInitialize (False)

end sub

'----------------------------------------------------------------
sub main()
	GetUserLicence(doc)
	SilkScreenRef_Move(doc)
end sub
'====================================================


